//
//  UserService.swift
//  jiaoyi
//
//  Created by wjk on 2018/9/10.
//  Copyright © 2018年 wjk. All rights reserved.
//

import Moya
import Result

import Foundation

enum UserService {
    case message(page:Int, per_page:Int)
    case enterpriseInfo
    case banner()
    case fundDetail
    case download(url:String)
    
}

private let DefaultDownloadDestination: DownloadDestination = { temporaryURL, response in
    return (DefaultDownloadDir.appendingPathComponent(response.suggestedFilename!), [])
}

//默认下载保存地址（用户文档目录）
let DefaultDownloadDir: URL = {
    let directoryURLs = FileManager.default.urls(for: .documentDirectory,
                                                 in: .userDomainMask)
    return directoryURLs.first ?? URL(fileURLWithPath: NSTemporaryDirectory())
}()

extension UserService :TargetType {
    
    
    
    var baseURL: URL {
        switch self {
        case .download(let url):
            return URL.init(string: url)!
        default:
            return URL.init(string: AppConstants.apiEndpoint)!
        }
        
    }
    
    var path: String {
        switch self {
        case .message(_,_):
            return "/api/v1/message"
        
        case .enterpriseInfo:
            return "/api/v1/user/enterprise-info"
        case .fundDetail:
            return "/api/v1/fund/account-detail"
        case .banner():
            return ""
        case .download(_): return ""
            
        }
        
    }
    
    var method: Moya.Method {
        return .get
    }
    
    var sampleData: Data {
        return "暂无数据".data(using: String.Encoding.utf8)!
    }
    
    var task: Task {
        switch self {
        case .message(let page, let per_page):
            return Task.requestParameters(parameters: ["page":page,"per_page":per_page], encoding: URLEncoding.default)
        case .download(_):
            //定义下载的DownloadDestination（不改变文件名，同名文件不会覆盖）
            
            return .downloadDestination(DefaultDownloadDestination)
        default:
            return Task.requestPlain
        }
        
    }
    
    var headers: [String : String]? {
        return NetWorkManager.shared.headers()
    }
}
